import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

import router from "@/router";
import store from "@/store";

const whiteList = ["/login", "/404"];
router.beforeEach(async(to, from, next) => {
    // nprogress.start()
    const token = store.state.user.token;
    if (token) {
        if (to.path === "/login") {
            next("/");
            nprogress.done()
        } else {
            if (!store.state.user.userInfo.userId) {
                await store.dispatch('user/getUserInfo')
            }
            next();
        }
    } else {
        if (whiteList.includes(to.path)) {
            next();
        } else {
            next("/login");
            nprogress.done()
        }
    }
});

router.afterEach(() => {
    // nprogress.done()
});